﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SwitchMiner.Models;
using System.Windows.Forms;

namespace SwitchMiner.Controlers
{
    public static class ConfigManager
    {

        public static List<MinerModel> LoadMiners(string inputFilename)
        {
            List<MinerModel> returnList = new List<MinerModel>();

            using(StreamReader inputMinerDataStream = new StreamReader(inputFilename, Encoding.Unicode))
            {
                if (inputMinerDataStream != null)
                {
                    while (inputMinerDataStream.EndOfStream == false)
                    {
                        string currentLine = inputMinerDataStream.ReadLine();
                        char[] delimiterChars = { ';' };
                        string[] currentSplitLine = currentLine.Split(delimiterChars, StringSplitOptions.None);
                        if (currentSplitLine.Length >= 2)
                        {
                            returnList.Add(new MinerModel(currentSplitLine[0], currentSplitLine[1]));
                        }
                    }
                }
            }

            return returnList;
        }
        public static List<ParameterModel> LoadParameters(string inputFilename)
        {
            List<ParameterModel> returnList = new List<ParameterModel>();

            using(StreamReader inputDataStream = new StreamReader(inputFilename, Encoding.Unicode))
            {
                if (inputDataStream != null)
                {
                    while (inputDataStream.EndOfStream == false)
                    {
                        string currentLine = inputDataStream.ReadLine();
                        char[] delimiterChars = { ';' };
                        string[] currentSplitLine = currentLine.Split(delimiterChars, StringSplitOptions.None);
                        if (currentSplitLine.Length >= 2)
                        {
                            returnList.Add(new ParameterModel(currentSplitLine[0], currentSplitLine[1]));
                        }
                    }
                }
            }

            return returnList;
        }
        public static List<SwitchModel> LoadSwitch(string inputFilename)
        {
            List<SwitchModel> returnList = new List<SwitchModel>();
            using(StreamReader inputDataStream = new StreamReader(inputFilename, Encoding.Unicode))
            {
                if (inputDataStream != null)
                {
                    while (inputDataStream.EndOfStream == false)
                    {
                        string currentLine = inputDataStream.ReadLine();
                        char[] delimiterChars = { ';' };
                        string[] currentSplitLine = currentLine.Split(delimiterChars, StringSplitOptions.None);
                        if (currentSplitLine.Length >= 3)
                        {
                            long lDuration = 60;
                            long.TryParse(currentSplitLine[2], out lDuration);
                            returnList.Add(new SwitchModel(currentSplitLine[0], currentSplitLine[1], lDuration));
                        }
                    }
                }
            }
            return returnList;
        }

        public static void SaveMinerConfig(string outputFilename, List<MinerModel> data)
        {
            
            try
            {
                using(StreamWriter outputStream = new StreamWriter(outputFilename, false, Encoding.Unicode))
                {
                    if (outputStream != null && data.Count > 0)
                    {
                        foreach (MinerModel model in data)
                        {
                            outputStream.WriteLine(model.MinerName + ";" + model.MinerPath);
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please make sure file " + outputFilename + " is accessible", "Cannot save changes", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public static void SaveParametersConfig(string outputFilename, List<ParameterModel> data)
        {
            try
            {
                using (StreamWriter outputStream = new StreamWriter(outputFilename, false, Encoding.Unicode))
                {
                    if (outputStream != null && data.Count > 0)
                    {
                        foreach (ParameterModel model in data)
                        {
                            outputStream.WriteLine(model.Name + ";" + model.Parameters);
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please make sure file " + outputFilename + " is accessible", "Cannot save changes", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public static void SaveSwitchConfig(string outputFilename, List<SwitchModel> data)
        {
            try
            {
                using(StreamWriter outputStream = new StreamWriter(outputFilename, false, Encoding.Unicode))
                {
                    if (outputStream != null && data.Count > 0)
                    {
                        foreach (SwitchModel model in data)
                        {
                            outputStream.WriteLine(model.MinerName + ";" + model.ParameterName + ";" + model.Duration);
                        }
                        outputStream.Flush();
                        outputStream.Close();
                        outputStream.Dispose();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please make sure file " + outputFilename + " is accessible", "Cannot save changes", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
